//-----------------------------------------------------------------------
//<copyright file="DonViTinhDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class DonViTinhDAO : SQLDatabase, IDonViTinhDAO
	{

		#region Create DonViTinh methods

		public DonViTinh CreateDonViTinh(DonViTinh donViTinh)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spDonViTinhInsert");
				AddOutParameter(command, "@MaDonVi", SqlDbType.NVarChar, donViTinh.MaDonVi);
				AddInParameter(command, "@TenDonVi", SqlDbType.NVarChar, donViTinh.TenDonVi);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, donViTinh.GhiChu);
				ExecuteNonQuery(command);
                donViTinh.MaDonVi = (string)GetParameterValue(command, "@MaDonVi");
				return donViTinh;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateDonViTinhException", ex);
			}
		}

		#endregion

		#region Update DonViTinh methods

		public void UpdateDonViTinh(DonViTinh donViTinh)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spDonViTinhUpdate");

				AddInParameter(command, "@MaDonVi", SqlDbType.NVarChar, donViTinh.MaDonVi);
				AddInParameter(command, "@TenDonVi", SqlDbType.NVarChar, donViTinh.TenDonVi);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, donViTinh.GhiChu);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateDonViTinhException", ex);
			}
		}

		#endregion

		#region Delete DonViTinh methods

		public void DeleteDonViTinh(DonViTinh donViTinh)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spDonViTinhDelete");

				AddInParameter(command, "@MaDonVi", SqlDbType.NVarChar, donViTinh.MaDonVi);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteDonViTinhException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll DonViTinh methods

		public DonViTinhs GetAllDonViTinh()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spDonViTinhGetList");
				DonViTinhs Result = new DonViTinhs();
				List<DonViTinh> List = ExecuteQuery<DonViTinh>(command,true);
				foreach(DonViTinh item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllDonViTinhException", ex);
			}
		}

		#endregion


		#region GetDonViTinh methods

		public DonViTinh GetDonViTinhByID(string maDonVi)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spDonViTinhGet");

				AddInParameter(command, "@maDonVi", SqlDbType.NVarChar, maDonVi);

				return ExecuteQuery<DonViTinh>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetDonViTinhBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
